<template>
  <div>
    <el-dialog
      :title="obj.isAdd == true ? '轮播图添加' : '轮播图编辑'"
      :visible.sync="obj.isTrue"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="changeimg"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button @click="submit()" v-if="obj.isAdd == true">添 加</el-button>
        <el-button type="primary" v-else @click="dits()">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { secondRouter } from "../../../router/index.js";
import { successAlert, warningAlert } from "../../../util/alter.js";
import {
  bannerAdd,
  bannerInfo,
  bannerEdit,
  
} from "../../../requster/api.js";
import { mapGetters, mapActions } from "vuex";
export default {
  //接收父组件传来的参数
  props: ["obj"],
  data() {
    return {
      imgUrl: "",
      form: {
        title: "",
        img: "",
        status: 1,
      },
     

      //菜单地址
      secondRouter: secondRouter,
    };
  },
  computed: {
    ...mapGetters({
      bannerList: "banner/list",
    }),
  },
  methods: {
    changeimg(e) {
      console.log(e);
      this.imgUrl = URL.createObjectURL(e.raw);
      this.form.img = e.raw;
    },

    ...mapActions({
      reqbannerList: "banner/reqChangeList",
    }),

    empty() {
      this.imgUrl = "";
      //清空封装
      this.form = {
        title: "",
        img: "",
        status: 1,
      };
    },
    cancel() {
      //取消事件
      //改变父组件的传来的isTure
      this.obj.isTrue = false;

      this.empty();
    },
    submit() {
      console.log(123);
      //点击添加按钮
      console.log(this.form);
      bannerAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqbannerList();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 获取一条数据
    getone(id) {
      bannerInfo({ id: id }).then((res) => {
        this.form = res.data.list;
        // 给form加id 确定编辑时没有id
        this.form.id = id;
        this.imgUrl = this.$pre + this.form.img;
      });
    },
    dits() {
      bannerEdit(this.form).then((res) => {
        console.log(this.form);
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqbannerList();
          this.cancel();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>